package com.hrbu.ssm.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrbu.ssm.bean.Dept;
import com.hrbu.ssm.bean.Employee;
import com.hrbu.ssm.bean.SearchBean;
import com.hrbu.ssm.request.RespBody;
import com.hrbu.ssm.request.Result;
import com.hrbu.ssm.service.DeptService;
import com.hrbu.ssm.service.EmployeeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/3/25
 * @Blog: https://tanyajun.top
 */
@Controller
@RequestMapping("emp")  //配置根路径
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DeptService deptService;

    @GetMapping("emplist")
    public ModelAndView toEmpList(ModelAndView modelAndView) {
        modelAndView.setViewName("emplist");
        modelAndView.addObject("list", employeeService.getEmployeeList());
        return modelAndView;
    }

    /**
     * 前往成员管理
     *
     * @return
     */
    @GetMapping("")
    public String gotoEmpPage() {
        return "emp/emp";
    }

    @GetMapping("eform")
    public String gotoEmpFormPage(@Param("id") Integer id, Model model) {
        if (id != null && id > 0) {
            Employee employee = employeeService.findEmpById(id);
            List<Dept> deptList = deptService.getDeptList();
            // 用于相应jsp的表单回显
            model.addAttribute("emp", employee);
            // 部门信息将加载到响应jsp页面中的下拉选择框
            model.addAttribute("deptList", deptList);
            System.out.println("进入eform页面获取到成员信息:" + employee + "\n" + deptList);
        }
        return "emp/eform";
    }

    /**
     * 分页获取成员列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("empList")
    @ResponseBody
    public RespBody getEmpList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        System.out.println(page + "," + limit);
        List<Employee> empList = employeeService.getEmpListByPage(page, limit);
        Long total = employeeService.getTotal();
        return new RespBody("获取分页成员列表:[ " + page + ", " + limit + " ]", 0, total, empList);
    }

    /**
     * 更新成员信息
     *
     * @param employee
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public Result updateEmp(@RequestBody Employee employee) {
        System.out.println("更新操作:" + employee);
        Integer num = employeeService.update(employee);
        if (num == 0) {
            return Result.error(0).setMessage("修改失败");
        }
        return Result.ok(1).
                setMessage("成员【" + employee.getId() + ", " + employee.getFirstName() + employee.getLastName() + "】: 信息修改成功");
    }

    /**
     * 删除成员：根据id删除
     *
     * @param employee
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Result deleteEmp(@RequestBody Employee employee) {
        System.out.println("删除操作:" + employee);
        Integer num = employeeService.delete(employee);
        if (num == 0) {
            return Result.error(0).setMessage("删除失败");
        }
        return Result.ok(1).setMessage("成员ID-" + employee.getId() + "=" + employee.getFirstName() + employee.getLastName() + ": 删除成功");
    }

    @GetMapping("search")
    @ResponseBody
    public RespBody search(SearchBean searchBean, Integer page, Integer limit) {
        System.out.println("模糊查询条件:" + searchBean);
        String startDate = null;
        String endDate = null;
        if (searchBean != null && searchBean.getDateRange() != null && !"".equals(searchBean.getDateRange())) {
            String dateRange = searchBean.getDateRange();
            int i = dateRange.indexOf("~");
            startDate = dateRange.substring(0, i).trim();
            endDate = dateRange.substring(i + 1).trim();
        }
        Integer total = employeeService.queryByMindTotalNum(searchBean.getEmpName(), startDate, endDate);
        List<Employee> employeeList = employeeService.queryByMind(searchBean.getEmpName(), startDate, endDate, page, limit);
        return new RespBody("查询成功", 0, Long.parseLong(total + ""), employeeList);
    }

    @GetMapping("addEmpform")
    public String gotoAddEmpform(Model model) {
        model.addAttribute("deptList", deptService.getDeptList());
        return "emp/addEmpform";
    }

    @PostMapping("insert")
    @ResponseBody
    public Result insertEmp(Employee employee) {
        System.out.println("添加操作:" + employee);
        Integer num = employeeService.insert(employee);
        if (num == 0) {
            return Result.error(0).setMessage("添加失败");
        }
        System.out.println(employee.getId());
        return Result.ok(1).setMessage("成员ID-" + employee.getId() + "=" + employee.getFirstName() + employee.getLastName() + ": 添加成功");
    }
}
